package com.mall.user.services;

import com.mall.user.IVerifyUserService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.entitys.UserVerify;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dal.persistence.UserVerifyMapper;
import com.mall.user.dto.UserVerifyRequest;
import com.mall.user.dto.UserVerifyResponse;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Component

public class VerifyUserServiceImpl implements IVerifyUserService {

    @Autowired
    UserVerifyMapper userVerifyMapper;
    @Autowired
    MemberMapper memberMapper;

    @Override
    public UserVerifyResponse verifyUser(UserVerifyRequest request) {
        UserVerifyResponse response = new UserVerifyResponse();
        try {
            request.requestCheck();
        } catch (Exception e) {
            response.setCode(SysRetCodeConstants.REQUEST_CHECK_FAILURE.getCode());
            response.setMsg(SysRetCodeConstants.REQUEST_CHECK_FAILURE.getMessage());
            return response;
        }
        Example example = new Example(UserVerify.class);
        example.createCriteria().andEqualTo("uuid", request.getUuid());
        List<UserVerify> userVerifies = userVerifyMapper.selectByExample(example);
        if (userVerifies.size() > 1) {
            response.setCode(SysRetCodeConstants.DATA_REPEATED.getCode());
            response.setMsg(SysRetCodeConstants.DATA_REPEATED.getMessage());
            return response;
        }
        if (!userVerifies.get(0).getUsername().equals(request.getUserName()) || userVerifies.size() == 0) {
            response.setCode(SysRetCodeConstants.DATA_NOT_EXIST.getCode());
            response.setMsg(SysRetCodeConstants.DATA_NOT_EXIST.getMessage());
            return response;
        }
        UserVerify verify = new UserVerify();
        verify.setId(userVerifies.get(0).getId());
        verify.setIsVerify("Y");
        int updateUserVerify = userVerifyMapper.updateByPrimaryKeySelective(verify);
        if (updateUserVerify != 1) {
            response.setCode(SysRetCodeConstants.SYSTEM_ERROR.getCode());
            response.setMsg(SysRetCodeConstants.SYSTEM_ERROR.getMessage());
            return response;
        }
        Member member = new Member();
        member.setIsVerified("Y");
        Example updateExample = new Example(Member.class);
        updateExample.createCriteria().andEqualTo("username", userVerifies.get(0).getUsername());
        int updateMember = memberMapper.updateByExampleSelective(member, updateExample);
        if (updateMember != 1) {
            response.setCode(SysRetCodeConstants.SYSTEM_ERROR.getCode());
            response.setMsg(SysRetCodeConstants.SYSTEM_ERROR.getMessage());
            return response;
        }
        response.setCode(SysRetCodeConstants.SUCCESS.getCode());
        response.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        return response;

    }
}
